package com.team7.home.service.impl;

import com.team7.common.entity.Banner;
import com.team7.common.vo.BannerVo;
import com.team7.common.vo.CommonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team7.home.dao.BannerDao;
import com.team7.home.service.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author 张煜杰
 * @since 2021-10-19
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerDao, Banner> implements BannerService {
    @Resource
    private BannerDao bannerDao;

    @Override
    public CommonResult getAllHeadNar(Integer page, Integer limit, BannerVo bannerVo) {
        //获取分页插件
        Page<Banner> bannerPage = new Page<>(page, limit);
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.isNotEmpty(bannerVo.getTitle())){
            wrapper.like("title",bannerVo.getTitle());
        }
        if (bannerVo.getIsDisable()!=null){
            wrapper.eq("is_disable",bannerVo.getIsDisable());
        }
        Page<Banner> selectPage = bannerDao.selectPage(bannerPage,wrapper);
        if (selectPage.getRecords().size()>0){
            return new CommonResult(2000,"查询成功",selectPage);
        }else {
            return new CommonResult(5000, "暂无数据", null);
        }
    }

    @Override
    public CommonResult changeStatus(String id, Integer isDisable) {
        Banner banner = new Banner();
        banner.setId(id);
        banner.setIsDisable(isDisable);
        int i = bannerDao.updateById(banner);
        if (i>0){
            return new CommonResult(2000,"成功",null);
        }else {
            return new CommonResult(5000,"失败",null);
        }
    }

    @Override
    public CommonResult addHeadNav(BannerVo bannerVo) {
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bannerVo.getId())){
            //这里是更新数据的方法
            System.out.println("1111111");
            Banner banner = new Banner();
            banner.setId(bannerVo.getId());
            banner.setGmtCreate(bannerVo.getGmtCreate());
            banner.setGmtModified(bannerVo.getGmtModified());
            banner.setImageUrl(bannerVo.getImageUrl());
            banner.setLinkUrl(bannerVo.getLinkUrl());
            banner.setSort(bannerVo.getSort());
            banner.setTitle(bannerVo.getTitle());
            int i = bannerDao.updateById(banner);
            if (i>0){
                return new CommonResult(2000,"更新成功",null);
            }else {
                return new CommonResult(5000,"未知原因更新失败",null);
            }
        }else{
            //此时事新增的方法
            //这里是更新数据的方法
            String bannerVoTitle = bannerVo.getTitle();
            Banner banner = new Banner();
            banner.setGmtCreate(bannerVo.getGmtCreate());
            banner.setGmtModified(bannerVo.getGmtModified());
            banner.setImageUrl(bannerVo.getImageUrl());
            banner.setLinkUrl(bannerVo.getLinkUrl());
            banner.setSort(bannerVo.getSort());
            banner.setTitle(bannerVo.getTitle());
            banner.setIsDisable(0);
            banner.setIsDeleted(0);
            int i = bannerDao.insert(banner);
            if (i>0){
                return new CommonResult(2000,"添加成功",null);
            }else {
                return new CommonResult(5000,"未知原因添加失败",null);
            }
        }
    }

    @Override
    public CommonResult deleteHeadNav(String id) {
        Banner banner = new Banner();
        banner.setId(id);
        banner.setIsDeleted(1);
        int i = bannerDao.updateById(banner);
        if (i>0){
            return new CommonResult(2000,"成功",null);
        }else {
            return new CommonResult(5000,"失败",null);
        }
    }


    @Override
    public CommonResult getQianBanner() {
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        wrapper.eq("is_disable",0);
        wrapper.eq("is_deleted",0);
        List<Banner> banners = bannerDao.selectList(wrapper);
        return new CommonResult(2000,"查询成功",banners);
    }
}
